package com.ruoyi.web.controller.user;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.base.user.domain.Agent;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.web.service.IAgentService;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 代理信息Controller
 *
 * @author gdk
 * @date 2023-05-27
 */
@RestController
@AllArgsConstructor
@RequestMapping("/user/agent")
public class AgentController extends BaseController {
    private final IAgentService agentService;

    /**
     * 查询代理信息列表
     */
    @PreAuthorize("@ss.hasPermi('user:agent:list')")
    @GetMapping("/list")
    public TableDataInfo list(Agent agent) {
        startPage();
        List<Agent> list = agentService.selectAgentList(agent);
        return getDataTable(list);
    }

    /**
     * 导出代理信息列表
     */
    @PreAuthorize("@ss.hasPermi('user:agent:export')")
    @Log(title = "代理信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Agent agent) {
        List<Agent> list = agentService.selectAgentList(agent);
        ExcelUtil<Agent> util = new ExcelUtil<Agent>(Agent.class);
        util.exportExcel(response, list, "代理信息数据");
    }

    /**
     * 获取代理信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('user:agent:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(agentService.selectAgentById(id));
    }

    /**
     * 新增代理信息
     */
    @PreAuthorize("@ss.hasPermi('user:agent:add')")
    @Log(title = "代理信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Agent agent) {
        return toAjax(agentService.insertAgent(agent));
    }

    /**
     * 修改代理信息
     */
    @PreAuthorize("@ss.hasPermi('user:agent:edit')")
    @Log(title = "代理信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Agent agent) {
        return toAjax(agentService.updateAgent(agent));
    }

    /**
     * 删除代理信息
     */
    @PreAuthorize("@ss.hasPermi('user:agent:remove')")
    @Log(title = "代理信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(agentService.deleteAgentByIds(ids));
    }
}
